package controller;

import entity.Reply;
import entity.Topic;
import entity.Users;
import service.IReplyService;
import service.ITopicService;
import service.impl.ReplyService;
import service.impl.TopicService;
import until.Pageuu;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class PageIndex extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ITopicService itd = new TopicService();
        IReplyService ipd=new ReplyService();
        String num = request.getParameter("pageIndex");
        if (num == null) {
            num = "1";
        }
        int index = Integer.parseInt(num);
        int count = itd.getCount();
        int tto = Pageuu.getPageSize(count, Pageuu.PageSize);

        if (index < 1) {
            index = 1;
        }
        if (index > tto) {
            index = tto;
        }

        List<Topic> list = itd.getAll(Pageuu.PageSize, index);

        List<Reply> list1 =ipd.getAll();

        request.getSession().setAttribute("list",list);
        request.getSession().setAttribute("list1",list1);
        request.getSession().setAttribute("index",index);
        request.getSession().setAttribute("tto",tto);
        request.getRequestDispatcher("boards/main.jsp").forward(request,response);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
